package ace.cmp.i18n.webmvc.core.autoconfig;

import ace.cmp.i18n.api.AceLocaleResolver;
import ace.cmp.i18n.api.AceMessageSource;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.cmp.i18n.core.impl.DefaultMessageSource;
import ace.cmp.i18n.core.impl.DefaultMessageSourceAssertService;
import ace.cmp.i18n.webmvc.core.impl.DefaultLocaleResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;

/**
 * @author caspar
 * @date 2023/2/1 16:55
 */
@Configuration
public class I18nWebMvcCoreAutoConfig {

  @Bean
  @ConditionalOnMissingBean
  public AceLocaleResolver aceLocaleResolver(LocaleResolver localeResolver) {
    AceLocaleResolver aceLocaleResolver = new DefaultLocaleResolver(localeResolver);
    return aceLocaleResolver;
  }

  @Bean
  @ConditionalOnMissingBean
  public AceMessageSource aceMessageSource(
      MessageSource messageSource, AceLocaleResolver localeResolver) {
    AceMessageSource aceMessageSource = new DefaultMessageSource(messageSource, localeResolver);
    return aceMessageSource;
  }

  @Bean
  @ConditionalOnMissingBean
  public MessageSourceAssertService messageSourceAssertService(AceMessageSource aceMessageSource) {
    MessageSourceAssertService messageSourceAssertService =
        new DefaultMessageSourceAssertService(aceMessageSource);
    return messageSourceAssertService;
  }
}
